﻿namespace DepartmentEditForm
{
    using System;
    using System.Collections.Generic;

    using Microsoft.SharePoint;

    public class TargetFieldsDataCollectionBL
    {
        #region Fields

        List<DepartmentVisibleFields> _AvailableFields;

        #endregion Fields

        #region Properties

        public List<DepartmentVisibleFields> AvailableFields
        {
            get
            {
                if ( _AvailableFields == null )
                    _AvailableFields = new List<DepartmentVisibleFields>( );
                return _AvailableFields;
            }
            set
            {
                _AvailableFields = value;
            }
        }

        #endregion Properties

        #region Methods

        public void GetAllDepartmentSelectedFields( SPWeb storeWeb, string sourceDepartment )
        {
            DepartmentVisibleFields deptFields = new DepartmentVisibleFields( );

            SPList departmentFieldsView = storeWeb.Lists[ "Department Access Administration" ];
            SPQuery sourceUserDepartments = new SPQuery( );

            sourceUserDepartments.Query = "<Where><Eq><FieldRef Name='Department_x0020_Name' /><Value Type='Text'>" + sourceDepartment.Trim( ) + "</Value></Eq></Where><OrderBy><FieldRef Name='Target_x0020_List_x0020_Name' Ascending='True' /><FieldRef Name='Field_x0020_Name' Ascending='True' /></OrderBy>";
            SPListItemCollection allFieldViews = departmentFieldsView.GetItems( sourceUserDepartments );

            foreach ( SPItem item in allFieldViews )
            {
                deptFields = new DepartmentVisibleFields( );
                deptFields.SourceDepartmentName = sourceDepartment;
                deptFields.TargetDepartmentName = item[ "Target Department Name" ].ToString( ).Trim( );
                deptFields.TargetFieldName = item[ "Field Name" ].ToString( ).Trim( );
                deptFields.TargetListName = item[ "Target List Name" ].ToString( ).Trim( );
                deptFields.ReadOnly = false;
                AvailableFields.Add( deptFields );
            }
        }

        public string GetDepartmentAbbreviations( SPWeb storeWeb, string sourceDepartment )
        {
            string result = "";
            SPList departmentListValues = storeWeb.Lists[ "Department Administration List" ];
            SPQuery nonUserDepartments = new SPQuery( );

            nonUserDepartments.Query = "<Where><Eq><FieldRef Name='Department_x0020_Name' /><Value Type='Text'>" + sourceDepartment.Trim( ) + "</Value>   </Eq></Where>";
            nonUserDepartments.ViewFields = "<FieldRef Name='Department_x0020_Grid_x0020_Code'/>";
            SPListItemCollection otherDepartments = departmentListValues.GetItems( nonUserDepartments );
            if ( otherDepartments.Count == 0 )
                return result;
            else
                return result = otherDepartments[ 0 ][ "Department Grid Code" ].ToString( ).Trim( );
        }

        public void GetSourceTargetDepartmentSelectedFields( string siteURL, string sourceDepartment, string targetDepartment )
        {
            using ( SPSite storeSite = new SPSite( siteURL ) )
            {
                DepartmentVisibleFields deptFields = new DepartmentVisibleFields( );
                using ( SPWeb storeWeb = storeSite.OpenWeb( ) )
                {
                    SPList departmentFieldsView = storeWeb.Lists[ "Department Access Administration" ];
                    SPQuery sourceUserDepartments = new SPQuery( );

                    sourceUserDepartments.Query = "<Where><And><Eq><FieldRef Name='Department_x0020_Name' /><Value Type='Text'>" + sourceDepartment + "</Value></Eq><Eq><FieldRef Name='Target_x0020_List_x0020_Name' /><Value Type='Text'>" + targetDepartment + "</Value></Eq></And></Where><OrderBy><FieldRef Name='Target_x0020_List_x0020_Name' Ascending='True' /><FieldRef Name='Field_x0020_Name' Ascending='True' /></OrderBy>";

                    SPListItemCollection allFieldViews = departmentFieldsView.GetItems( sourceUserDepartments );

                    foreach ( SPItem item in allFieldViews )
                    {
                        deptFields = new DepartmentVisibleFields( );
                        deptFields.SourceDepartmentName = sourceDepartment;
                        deptFields.TargetDepartmentName = item[ "Target Department Name" ].ToString( ).Trim( );
                        deptFields.TargetFieldName = item[ "Field Name" ].ToString( ).Trim( );
                        deptFields.TargetListName = item[ "Target List Name" ].ToString( ).Trim( );
                        deptFields.ReadOnly = false;
                        AvailableFields.Add( deptFields );
                    }
                }
            }
        }

        public void SaveSelectedField( string siteURL, string sourceDepartment, string targetDepartment, string targetList, string fieldName )
        {
            SaveSelectedField( siteURL, sourceDepartment, targetDepartment, targetList, fieldName, false );
        }

        public void SaveSelectedField( string siteURL, string sourceDepartment, string targetDepartment, string targetList, string fieldName, bool readOnly )
        {
            using ( SPSite storeSite = new SPSite( siteURL ) )
            {
                using ( SPWeb storeWeb = storeSite.OpenWeb( ) )
                {
                    SPListItem item;
                    SPList departmentFieldsView = storeWeb.Lists[ "Department Access Administration" ];
                    item = departmentFieldsView.Items.Add( );
                    item[ "Title" ] = sourceDepartment;
                    item[ "Department Name" ] = sourceDepartment;
                    item[ "Target List Name" ] = targetList;
                    item[ "Field Name" ] = fieldName;
                    item[ "Target Department Name" ] = targetDepartment;

                    item.Update( );
                    departmentFieldsView.Update( );
                }
            }
        }

        #endregion Methods
    }
}